package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"shop/model/mysql"
	"shop/model/redisinit"

	"shop/shoprpc/internal/svc"
	"shop/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopLikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopLikeLogic {
	return &ShopLikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopLikeLogic) ShopLike(in *shoprpc.ShopLikeRequest) (*shoprpc.ShopLikeResponse, error) {
	// todo: add your logic here and delete this line

	var findshop mysql.Products
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&findshop)
	if findshop.ID == 0 {
		return nil, fmt.Errorf("该商品不存在，无法进行喜欢")
	}

	marshal, _ := json.Marshal(findshop)
	redisinit.Rdb.ZIncrBy("shoplike", 1, string(marshal))

	return &shoprpc.ShopLikeResponse{
		Id: int64(findshop.ID),
	}, nil
}
